import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/home.vue'
// @  是一个绝对路径 指向的是src 
import Login from '@/views/login/login.vue';
import Users from "@/views/users/users.vue";
import Rights from "@/views/rights/rights.vue";
import Welcome from "@/views/welcome/welcome.vue";
import Roles from "@/views/roles/roles.vue";
import Reports from "@/views/reports/reports.vue";
import Orders from "@/views/orders/orders.vue";
import Goods from "@/views/goods/goods.vue";
import Params from "@/views/params/params.vue";
import Categories from "@/views/categories/categories.vue";
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/welcome'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect:{
      path:'/welcome'
    },
    children:[
      {
        path: "/welcome",
        name: "welcome",
        component: Welcome,
    },{
      path:"/rights",
      name:'rights',
      component:Rights
    },
    {
      path: "/users",
      name: "users",
      component: Users,
    },
    {
      path: "/roles",
      name: "roles",
      component: Roles,
    },
    {
      path: "/reports",
      name: "reports",
      component: Reports,
    },
    {
      path: "/orders",
      name: "orders",
      component: Orders,
    },
    {
      path: "/goods",
      name: "goods",
      component: Goods,
    },
    {
      path: "/params",
      name: "params",
      component: Params,
    },
    {
      path: "/categories",
      name: "categories",
      component: Categories,
    }
  ]
  },
  
];

const router = new VueRouter({
  mode: 'history',
  routes
})
router.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem("myToken");
  //表示已登录
  if (token) {
    next();
  } else {
    //没有token  表示没有登录
    if (to.path == "/login") {
      next();
    } else {
     next({path:"login"});
    }
  }
});

export default router